package com.generic;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 持有特定类型对象列表，每次调用其上的select()方法时，随机选取一个元素
 * 
 * @author Administrator
 * 
 * @param <T>
 */
public class RandomList<T> {

	private List<T> storage = new ArrayList<T>();
	private Random rand = new Random(69);

	public void add(T item) {
		storage.add(item);
	}

	public T select() {
		return storage.get(rand.nextInt(storage.size()));
	}

	public static void main(String[] args) {
		RandomList<String> rs = new RandomList<String>();
		for (String s : "my name is zhang su lei".split(" ")) {
			rs.add(s);
		}

		System.out.println(rs.select());
	}

}
